package com.gkj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gkj.blog.business.dto.PageResult;
import com.gkj.blog.business.enums.ResponseStatus;
import com.gkj.blog.business.service.UserRoleService;
import com.gkj.blog.business.service.UserService;
import com.gkj.blog.business.vo.condition.UserConditionVO;
import com.gkj.blog.business.vo.entity.User;
import com.gkj.blog.framework.object.ResponseVO;
import com.gkj.blog.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;

    @PostMapping("/list")
    public PageResult list(UserConditionVO vo) {
        PageInfo<User> pageInfo = userService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }
    @RequestMapping("/listAll")
    public ResponseVO listAll(){
        UserConditionVO vo=new UserConditionVO();
        vo.setPageSize(1000);
        PageInfo<User> pageInfo = userService.findPageBreakByCondition(vo);
        return ResultUtil.success(null,pageInfo==null?null:pageInfo.getList());
    }
    /**
     * 设置用户角色
     * @param userId
     * @param roleIds
     * @return
     */
    @PostMapping("/saveUserRoles")
    public ResponseVO saveUserRoles(Long userId, @RequestParam("roleIds[]") Long[] roleIds) {
        if (StringUtils.isEmpty(userId)) {
            return ResultUtil.error("error");
        }
        userRoleService.setRoles(userId, roleIds);
        return ResultUtil.success("成功");
    }

    @PostMapping(value = "/add")
    public ResponseVO add(User user) {
        User u = userService.getUserByUsername(user.getUsername());
        if (u != null) {
            return ResultUtil.error("该用户名[" + user.getUsername() + "]已存在！请更改用户名");
        }
        try {
            userService.insert(user);
            return ResultUtil.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("error");
        }
    }

    @PostMapping(value = "/remove")
    public ResponseVO remove(@RequestParam("ids[]") Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
            userService.delete(id);
            userRoleService.removeByUserId(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个用户");
    }

    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, userService.select(id));
    }

    @PostMapping("/edit")
    public ResponseVO edit(User user) {
        try {
            userService.update(user);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("用户修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

}
